//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverExecuteActivate.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_act_id","_action","_arguments","_parameters","_player","_action_id","_current_status","_activated_msg","_deactivated_msg","_shared_variable","_initial_status","_activated_title","_deactivated_title","_activated_code","_deactivated_code"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		_activated_msg = [_parameters, "ACTIVATION_MSG", []] call CFUNC(hashGet);
		_deactivated_msg = [_parameters, "DEACTIVATION_MSG", []] call CFUNC(hashGet);
		_shared_variable = [_parameters, "SHARED_VAR", ""] call CFUNC(hashGet);
		_initial_status = [_parameters, "SHARED_VAR_VAL", false] call CFUNC(hashGet);
		_activated_title = [_parameters, "ACTIVATED_TITLE", ""] call CFUNC(hashGet);
		_deactivated_title = [_parameters, "DEACTIVATED_TITLE", ""] call CFUNC(hashGet);
		_activated_code = [_parameters, "ACTIVATED_CODE", ""] call CFUNC(hashGet);
		_deactivated_code = [_parameters, "DEACTIVATED_CODE", ""] call CFUNC(hashGet);
		
		if (typename _activated_msg == typename "") then {_activated_msg = [_activated_msg];};
		if (typename _deactivated_msg == typename "") then {_deactivated_msg = [_deactivated_msg];};
		
		_current_status = [_action,_initial_status] call FFUNC(actionGetEvent);
		_current_status = !_current_status;
		TRACE_1("New Status",_current_status);
		
		if (!isNil "_shared_variable") then {
			if (_shared_variable != "") then {
				[_instance_id,_shared_variable,_current_status] call FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_variable,_current_status],THIS_FILE_] call NFUNC(callEvent);
			};
		};
		_action = [_action, _current_status] call FFUNC(actionSetEvent);
		[_object, _action] call FFUNC(actionSet);
		if (_current_status) then {
			TRACE_2("Activation",_activated_msg,_activated_code);
			If (count _activated_msg > 0) then { [QGVAR(all_message),[_activated_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent); };
			if (_activated_code != "") then {
				[QGVAR(all_execute),[_activated_code,_object,_action],THIS_FILE_] call NFUNC(callEvent);
			};
		} else {
			TRACE_2("Deactivation",_deactivated_msg,_deactivated_code);
			If (count _deactivated_msg > 0) then { [QGVAR(all_message),[_deactivated_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent); };
			if (_deactivated_code != "") then {
				[QGVAR(all_execute),[_deactivated_code,_object,_action],THIS_FILE_] call NFUNC(callEvent);
			};
		};
	} else {
		ERROR("Action ID not set");
	};
} else {
	ERROR("Object not set");
};

// _object = _this select 0;
// _action = _this select 1;
// _parameters = _this select 2;
// _player = _this select 3;
// _action_id = _this select 4;

// _activated_msg = _parameters select 0;
// if (typename _activated_msg == typename "") then {_activated_msg = [_activated_msg];};
// _deactivated_msg = _parameters select 1;
// if (typename _deactivated_msg == typename "") then {_deactivated_msg = [_deactivated_msg];};
// _shared_variable = _parameters select 2;
// _activated_title = localize "STR_MF_ACTIVATE_DEFACTIONON";
// if (count _parameters > 3) then {_activated_title = _parameters select 3;};
// _deactivated_title = localize "STR_MF_ACTIVATE_DEFACTIONOFF";
// if (count _parameters > 4) then {_deactivated_title = _parameters select 4;};
// _initial_status = False;
// if (count _parameters > 5) then {_initial_status = _parameters select 5;};
// _activated_code = "";
// if (count _parameters > 6) then {_activated_code = _parameters select 6;};
// _deactivated_code = "";
// if (count _parameters > 7) then {_deactivated_code = _parameters select 7;};

// _current_status = _object getVariable "mission_object_activation_status";
// If (isNil "_current_status") then {_current_status = _initial_status};
// _current_status = !_current_status;
// _object setVariable ["mission_object_activation_status",_current_status, true];
// // We change the initial variable for the JIP players
// _parameters set [3,_activated_title];
// _parameters set [4,_deactivated_title];
// _parameters set [5,_current_status];
// [_object,_parameters] call XfMissionSetParameters;

// if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: obj:%4  actm:%5 deactm:%6  var:%7  ini:%8  actt:%9  deactt:%10  sts:%11  actc:%12  deactc:%13",diag_tickTime,"mission_functions.sqf","XfMissionObjectServerExecuteActivate",typeof _object, _activated_msg, _deactivated_msg, _shared_variable, _initial_status, _activated_title, _deactivated_title, _current_status, (_activated_code != ""),(_deactivated_code != "")];};
// if (!isNil "_shared_variable") then {
	// if (_shared_variable != "") then {
		// [_shared_variable,_current_status] call XNetSetJIP;
		// ["d_all_var", [_shared_variable,_current_status]] call XNetCallEvent;
	// };
// };
// if (_current_status) then {
	// if (count _activated_msg > 0) then {["d_msg",[_activated_msg,"all",objNull,"hq"]] call XNetCallEvent; };
	// if (_activated_code != "") then { ["d_all_exec",[_activated_code,_object]] call XNetCallEvent; };
// } else {
	// if (count _deactivated_msg > 0) then {["d_msg",[_deactivated_msg,"all",objNull,"hq"]] call XNetCallEvent; };
	// if (_deactivated_code != "") then {	["d_all_exec",[_deactivated_code,_object]] call XNetCallEvent; };
// };

TRACE("End");
TMFPROFILERSTOP;